package com.bdqn.controller;

import com.bdqn.LayUI.Layui;
import com.bdqn.Service.Material.MaterialService;
import com.bdqn.Service.Orders.OrdersService;
import com.bdqn.Service.SuppMaterial.SuppMaterialService;
import com.bdqn.Service.Supplier.SupplierService;
import com.bdqn.entity.*;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Controller
@RequestMapping("/Mater")
public class MaterialController {
    @Autowired
    MaterialService materialService;
    @Autowired
    OrdersService ordersService;
    @Autowired
    SupplierService supplierService;
    @Autowired
    SuppMaterialService suppMaterialService;
    /**
     *  进入需求计划录入
     */
    @RequestMapping("/")
    @ResponseBody
    public Layui<Material> SelectMat(@Param("limit") int limit,/*页大小*/
                                    @Param("page") int page/*页码*/){
        int page1 = (page - 1) * limit;
        List<Material> materials=materialService.findByMaterial(null,limit,page1);
        int count=materialService.count(null,limit,page);
        //新建layui分页需要的返回数据类型。
        Layui<Material> lists = new Layui<Material>();
        lists.setCode(0);
        lists.setCount(count);
        lists.setData(materials);
        return lists;
    }
    /**
     * 需求计划录入
     */
    @RequestMapping("/SelListId")
    @ResponseBody
    public Layui<Material> SelectListId(@Param("limit") Integer limit,/*页大小*/
                                        @Param("page") Integer page/*页码*/,@RequestParam(value = "ids[]",required = false) int[] ids,@Param("idd") Integer idd,@RequestParam(value = "idds[]",required = false) String[] idds,@RequestParam(value = "datasid[]",required = false)int[] datasid) throws InterruptedException {
        int page1 = (page - 1) * limit;
        List idss=new ArrayList();
        for (int id : ids) {
            idss.add(id);
        }
        List<Material> byListId = materialService.findByListId(limit,page1,idss);
        for (int i=0;i<byListId.size();i++) {
            byListId.get(i).setMstatic("未完成");
            if(idds!=null){
                byListId.get(i).setOrderId(idds[i]);
            }
        }
        int count=materialService.countListId(idss);
        Layui<Material> lists = new Layui<Material>();
        lists.setData(byListId);
        lists.setCode(0);
        lists.setCount(count);
        if(idd!=null){
            for (Material material : byListId) {
                if(datasid!=null){
                    for (int i = 0; i < datasid.length; i++) {
                        if(material.getId()==datasid[i]){
                            material.setMstatic("完成");
                        }
                    }
                }
                if(material.getId()==idd){
                    material.setMstatic("完成");
                }
            }
        }
        //需求录入
//        List<Material> List=materialService.findByListId(1000,0,idss);
        List<Orders> orderss=new ArrayList<Orders>();
        if(idd==null){
            for (Material material : byListId) {
                Orders orders = new Orders();
                orders.setMaterialCode(material.getMaterialNum());
                orders.setMaterialName(material.getMaterialName());
                orders.setMeasureUnit(material.getMaterialUnit());
                Calendar calendar=Calendar.getInstance();
                orders.setOrderNum("1000"+calendar.get(Calendar.YEAR)+calendar.get(Calendar.MONTH)+calendar.get(Calendar.DATE)+calendar.get(Calendar.HOUR_OF_DAY)+calendar.get(Calendar.MINUTE)+calendar.get(Calendar.SECOND));
                TimeUnit.SECONDS.sleep(1);
                material.setOrderId(orders.getOrderNum());
                orders.setProgress("未录入");
                orderss.add(orders);
            }
            ordersService.Insert(orderss);
        }else{
//            Orders orders = new Orders();
//            orders.setOrderNum(idd.toString());
//            Orders byId = ordersService.findById(orders);
//            Material material=new Material();
//            material.setId(material.getId());
//            material.setMaterialNum(material.getMaterialNum());
//            material.setMaterialName(material.getMaterialName());
//            material.setMstatic(material.getMstatic());
//            material.setOrderId(material.getOrderId());

        }
        return lists;
    }
    @RequestMapping("/findAll")
    @ResponseBody
    public Layui<Material> findAll(){
        List<Material> materials = materialService.findAll();
        Layui<Material> materialLayui = new Layui<>();
        materialLayui.setData(materials);
        materialLayui.setCode(0);
        return materialLayui;
    }


    /**
     * 添加功能的完成
     */
    @RequestMapping("/addSuppMaterial.html")
    @ResponseBody
    public  Layui<Material>  addSuppMaterial(Integer page, Integer limit,String name){
        //判断当前页是否为空如果为空那就为第一页
        if(page==null||page.equals("")){
            page=1;
        }
        //获得sql里面的
        if(limit==null||limit.equals("")){
            limit=10;
        }
        page= (page - 1) * limit;
        List<Material> all = materialService.selectmaterial(page,limit,name);
        Layui<Material> MaterialLayui = new Layui<>();
        MaterialLayui.setCode(0);
        MaterialLayui.setData(all);
        MaterialLayui.setCount(materialService.materialcount(name));//总数量，查
        System.out.println(MaterialLayui);
        return MaterialLayui;
    }
    /**
     * 添加功能的完成
     */
}
